package com.xyht.sca_s.student_manage_system.modules.asset.config;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xyht.sca_s.student_manage_system.modules.asset.entity.SmsAssetCategoryManagement;
import com.xyht.sca_s.student_manage_system.modules.asset.mapper.SmsAssetCategoryManagementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.xyht.sca_s.student_manage_system.modules.asset.constant.CategoryDeleteFlagConstant.CATEGORY_DELETE_FLAG_FALSE;
import static com.xyht.sca_s.student_manage_system.modules.asset.constant.CategoryDeleteFlagConstant.CATEGORY_DELETE_FLAG_TRUE;

@Component
public class AssetCategoryInit {
    @Autowired
    private SmsAssetCategoryManagementMapper smsAssetCategoryManagementMapper;

    private String[] assetArray = {"土地房屋构筑物", "教学实验仪器", "电教设备",
            "卫生医疗器械", "交通运输工具", "生产工具", "图书", "办公教学设备", "音体美设备"};

    @Bean(value = "categoryInit")
    public void categoryInit() {
        Integer categoryCount =
                smsAssetCategoryManagementMapper.selectCount(new QueryWrapper<SmsAssetCategoryManagement>()
                        .lambda()
                        .eq(SmsAssetCategoryManagement::getParentId, 0));
        if (categoryCount == 0){
            int id = 1;
            for (String assetName:assetArray) {
                SmsAssetCategoryManagement smsAssetCategoryManagement = new SmsAssetCategoryManagement();
                smsAssetCategoryManagement.setCategoryName(assetName);
                smsAssetCategoryManagement.setParentId("0");
                smsAssetCategoryManagement.setChildNum(0);
                smsAssetCategoryManagement.setRootParentId(id+"");
                smsAssetCategoryManagement.setId(id+"");
                smsAssetCategoryManagement.setIsCanDelete(CATEGORY_DELETE_FLAG_FALSE);

                smsAssetCategoryManagementMapper.insert(smsAssetCategoryManagement);
                id++;
            }
        }
    }

}
